import { segment } from "oicq";
import fetch from "node-fetch";
const _path = process.cwd();
export const rule = {
  beian: {
    reg: "^#查备案(.*)$", 
    priority: 5000,
    describe: "查看网站备案信息", 
  },
  ping: {
    reg: "^#ping(.*)$",
    priority: 5000,
    describe: "ping", 
  },
};
export async function beian(e) {
  let msg = e.msg;
  msg = msg.replace(/#|查备案/g, "").trim();
  let url = `https://api.vvhan.com/api/icp?url=${(msg)}`;
  let response = await fetch(url);
  let res = await response.json();
  if (res.message == "此域名未备案") {
    e.reply("此域名未备案，或未在国内备案");
    return true;
  }
  if (res.message == "请输入正确的域名") {
    e.reply("请输入正确的域名");
    return true;
  }
  if (res.message == "参数输入不完整") {
    e.reply("请在#查备案后带上正确的域名");
    return true;
  }
  if (!res.success) {
    e.reply("查询失败");
    return true;
  }
  msg = [
    "主办单位名称：",
    segment.text(res.info.name),
    "\n",
    "主办单位性质：",
    segment.text(res.info.nature),
    "\n",
    "网站备案、许可证号：",
    segment.text(res.info.icp),
    "\n",
    "网站名称：",
    segment.text(res.info.title),
    "\n",
    "审核时间：",
    segment.text(res.info.time),
    "\n",
    "网站首页：",
    segment.text(res.domain),
  ];

  e.reply(msg);
  return true;
}
export async function ping(e) {
  let msg = e.msg;
  msg = msg.replace(/#|ping/g, "").trim();
  e.reply(`ping ${(msg)}中......`);   
  let num = 5;
  let url = `http://ovooa.com/API/ping/?url=${(msg)}&num=${(num)}`;
  let response = await fetch(url);
  let res = await response.json();
  if (res.code == -1) {
    msg = "www." + msg;
    url = `http://ovooa.com/API/ping/?url=${(msg)}&num=${(num)}`;
    response = await fetch(url);
    res = await response.json();
    if (res.code == -1) {
      e.reply("请输入正确的网址");
      return true;
    }

  }
  msg = [
    segment.text(res.data.url),
    "\n",
    "IP:",
    segment.text(res.data.IP),
    "\n",
    "IP属地：",
    segment.text(res.data.address),
    "\n",
    "最小延迟：",
    segment.text(res.data.small), "ms",
    "\n",
    "最大延迟：",
    segment.text(res.data.max), "ms",
    "\n",
    "平均延迟：",
    segment.text(res.data.average), "ms",
    "\n",
    "发送数据包：：",
    segment.text(res.data.num), "个",
    "\n",
    "接收数据包：：",
    segment.text(res.data.receive), "个",
    "\n",
    "丢包率：",
    segment.text(res.data.abandon),
    "\n",
    "总耗时：",
    segment.text(res.data.Times), "ms"
  ];
  e.reply(msg);
  return true;
}